"use strict";
/* Controllers */
app.controller("smartLogListController", ['$scope', '$location', '$rootScope', '$routeParams', 'smartLogListService', function ($scope, $location, $rootScope, $routeParams, smartLogListService) {

    var vm = $scope.vm = {};
    vm.p = {};
    vm.result = {};
    vm.p.appID = $routeParams.appID; //  获取应用模块的名称

    var errorFn = function (result) {
        alert(result);
    };
    vm.conf = [];

    $scope.queryItems = function(id) {
        $scope.listMetricSource(id);
    };

    $scope.searchItems = function() {
        var vm = $scope.vm;
        vm.result.ms = "...";
        // 去除两端空格
        vm.p.search = vm.p.search.replace(/^\s+|\s+$/g,'');
        if(vm.p.search=='') {
            alert("请输入搜索表名称");
            return;
        }
        var params = {search: vm.p.search};
        var procFn = function (response) {
            if ("000" !== response.code) {
                alert(response.message);
                return ;
            }
            vm.result.ms = response.ms;
            var data = response.data;
            if (data == null) {
                return;
            }
            vm.result.data = data;
//            if(vm.p.search!='') {
//                $('#list_table').bootstrapTable('showColumn', 'queryComment');
//            }
            $('#list_table').bootstrapTable('load', data);
        };

        smartLogListService.listQueryCmd(params, procFn, errorFn);
    }

    $scope.listQueryCmd = function (topic) {
        var vm = $scope.vm;
        vm.result.ms = "...";
        var params = {topic: topic};
        var procFn = function (response) {
            if ("000" !== response.code) {
                alert(response.message);
                return ;
            }
            vm.result.ms = response.ms;
            var data = response.data;
            if (data == null) return;
            vm.result.data = data;
            $('#list_table').bootstrapTable('load', data);
        };
        smartLogListService.listQueryCmd(params, procFn, errorFn);
    };


    $scope.initializeController = function () {
        vm.result.ms = '';

        var $resultTable = $('#list_table');
        $resultTable.bootstrapTable({
             pageList: [30, 50, 100, 300, 500, 1000], pageSize: 30
        });

//        $resultTable.bootstrapTable('hideColumn', 'queryCmd');
//        $resultTable.bootstrapTable('hideColumn', 'queryComment');
//        $resultTable.bootstrapTable('hideColumn', 'queryParam');
//        $resultTable.bootstrapTable('hideColumn', 'lastOperator');

        $scope.listQueryCmd();
    };

    $scope.initializeController();

}]);


/* Services */
app.service('smartLogListService', ['ajaxService', function (ajaxService) {

    this.listQueryCmd = function (params, successFunction, errorFunction) {
        ajaxService.AjaxPost(params, "/star/api/basic-service/v1/smartdq/listQueryCmd.do", successFunction, errorFunction);
    };

}]);



function queryCmdIDFmt(value, row, index) {
    if (value == null) {
        return "-";
    }
    return [
        '<a href="#/smart/query/smart_query_item/id/', row.id,'" target="_blank">', "<u>详情</u>" , '</a> ',
        '<a href="#/smart/query/smart_query_add/id/', row.id,'" target="_blank">', "<u>修改</u>" , '</a>'
    ].join('');
}


function queryCodeFmt(value, row, index) {
    if (value == null) {
        return "-";
    }
    return [
        '<a href="#/smart/query/smart_query_item/code/', row.queryCode,'" target="_blank">', " <u>" +value+"</u>" , '</a>'
    ].join('');
}

function evnStatusFmt(value, row, index) {
    if (value == null) {
        return "-";
    }
    var ret = '';
    switch(value) {
    case 0:
        ret = '新建';
        break;
    case 1:
        ret = 'PRE';
        break;
    case 2:
        ret = 'WWW';
        break;
    case 3:
        ret = '停用';
        break;
    default:
        ret = '未知'+value;
        break;
    }
    return ret;
}



// 格式为： {表描述},{表备注}
// 表描述： 即表是什么 ,表备注：即表的计算逻辑以及需要注意项
function logicTableCommentFmt(value, row, index) {
    if (value == null) {
        return "-";
    }
    var items = value.split(/,|，|。/);
    value = items[0];

    var result = [];
    var MAX_LEN= 30;
    for(var i=0;i<value.length;) {
        var item = value.substr(i, MAX_LEN);
        result.push(item);
        i += MAX_LEN;
    }
    return result.join('<br/>');
}



